/**
 * 
 */
package com.antilia.infomaresme.data.dataobjects;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.persistence.Entity;

/**
 * @author Ernesto
 *
 */
@Entity
public class Item extends DomainObject {

	private static final long serialVersionUID = 1L;

	private String title;
	
	
	private String description;
	
	/**
	 * String representing a (business) category path for the item.
	 * 
	 * (e.g. >>1>>12>34>>)
	 */
	private String categoryPath;
	
	private Long category1;
	
	private Long category2;
	
	private Long category3;
	
	private Long category4;
	
	private Long category5;
	
	private Long category6;
	
	private String categoryTitle;
	
	private Double price;
	
	/**
	 * String representing a (geographic) category path for the item.
	 * 
	 * (e.g. >>1>>12>34>>)
	 */
	private String geographicPath;
	
	
	private String geographicLocation;
	
	/**
	 * A comma separated list of IDs of image details.
	 */
	private String imageDetails;
	
	/**
	 * Constructor.
	 */
	public Item() {
	}

	public String getCategoryPath() {
		return categoryPath;
	}

	public void setCategoryPath(String categoryPath) {
		this.categoryPath = categoryPath;
	}
	
	public static String buildCategory(Category category) {
		if(category == null)
			return "";
		StringBuffer buffer = new StringBuffer();
		do {
			buffer.append(">>");
			buffer.append(category.getId());
			category = category.getParent();
		} while (category != null);
		buffer.append(">>");
		return buffer.toString();
	}
	
	public static String buildTitle(Category category) {
		if(category == null)
			return "";
		StringBuffer buffer = new StringBuffer();
		do {
			buffer.append(">>");
			buffer.append(category.getTitle());
			category = category.getParent();
		} while (category != null);
		buffer.append(">>");
		return buffer.toString();
	}

	public void asignCategories(Category category) {
		if(category == null)
			return;
		Category parent = category;
		int depth = category.getDepth();
		while (parent != null) {
			switch(depth) {
			 	case 1: {
			 		setCategory1(parent.getId());
			 		break;
			 	}
			 	case 2: {
			 		setCategory2(parent.getId());
			 		break;
			 	}
			 	case 3: {
			 		setCategory3(parent.getId());
			 		break;
			 	}
			 	case 4: {
			 		setCategory4(parent.getId());
			 		break;
			 	}
			 	case 5: {
			 		setCategory5(parent.getId());
			 		break;
			 	}
			 	case 6: {
			 		setCategory6(parent.getId());
			 		break;
			 	}
			}
			parent = parent.getParent();
			depth--;
		}
	}
	
	public void setCategoryPath(Category category) {
		this.categoryPath = buildCategory(category);
		this.categoryTitle = buildTitle(category);
		asignCategories(category);
	}

	public String getGeographicPath() {
		return geographicPath;
	}

	public void setGeographicPath(String geographicPath) {
		this.geographicPath = geographicPath;
	}
	
	public void setGeographicPath(Category category) {
		this.geographicPath = buildCategory(category);
		this.geographicLocation = buildTitle(category);		
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Item other = (Item) obj;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}

	public String getCategoryTitle() {
		return categoryTitle;
	}

	public void setCategoryTitle(String categoryTitle) {
		this.categoryTitle = categoryTitle;
	}

	public String getGeographicLocation() {
		return geographicLocation;
	}

	public void setGeographicLocation(String geographicLocation) {
		this.geographicLocation = geographicLocation;
	}

	public Long getCategory1() {
		return category1;
	}

	public void setCategory1(Long category1) {
		this.category1 = category1;
	}

	public Long getCategory2() {
		return category2;
	}

	public void setCategory2(Long category2) {
		this.category2 = category2;
	}

	public Long getCategory3() {
		return category3;
	}

	public void setCategory3(Long category3) {
		this.category3 = category3;
	}

	public Long getCategory4() {
		return category4;
	}

	public void setCategory4(Long category4) {
		this.category4 = category4;
	}

	public Long getCategory5() {
		return category5;
	}

	public void setCategory5(Long category5) {
		this.category5 = category5;
	}

	public Long getCategory6() {
		return category6;
	}

	public void setCategory6(Long category6) {
		this.category6 = category6;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public String getImageDetails() {
		return imageDetails;
	}
	
	public List<String> getImageDetailsAsList() {
		if(imageDetails== null || imageDetails.trim().length()==0)
			return new ArrayList<String>();
			
		StringTokenizer td = new StringTokenizer(imageDetails, ",");
		ArrayList<String> images = new ArrayList<String>();
		while(td.hasMoreTokens()) {
			images.add(td.nextToken());			
		}
		return images;
	}

	public void setImageDetails(String imageDetails) {
		this.imageDetails = imageDetails;
	}
	
}
